<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    var socket;
    // 判断当前浏览器是否支持 websocket
    if (window.WebSocket) {
        // go on
        socket = new WebSocket("ws://localhost:7000/hello");
        // 相当于 channelReadio ev 收到服务器回送的消息
        socket.onmessage = function (ev) {
            var rt = document.getElementById("responseText");
            rt.value = rt.value + "\n" + ev.data;
        }

        // 相当与连接开启
        socket.onopen = function (ev) {
            var rt = document.getElementById("responseText");
            rt.value = "连接开启。。";
        }

        // 相当与连接关闭
        socket.onclose = function (ev) {
            var rt = document.getElementById("responseText");
            rt.value += "\n 连接关闭。。"

        }
    } else {
        alert("当前浏览器不支持 webSocket")
    }
    //发送消息到服务器
    function send(message) {
        if (!window.socket) { // 先判断 socket 是否创建好
            return;
        }
        if (socket.readyState == WebSocket.OPEN) {
            // 通过 socket 发送消息
            socket.send(message)
        } else {
            alert("连接未开启");
        }
    }
</script>
    <form onsubmit="return false">
        <textarea id="message" name="message" style="height: 300px; width: 300px"></textarea>
        <input type="button" value="发生消息" onclick="send(this.form.message.value)">
        <textarea id="responseText" name="responseText" style="height: 300px; width: 300px"></textarea>
        <input type="button" value="清空内容" onclick="document.getElementById('responseText').value=''">
    </form>
</body>
</html>